package com.shop2cn.iapi.prodstock.testcase;

import java.io.IOException;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.activitymgt.parameter.AddactivityBean;
import com.shop2cn.iapi.prodstock.parameter.OrderReturnCatalogDto;
import com.shop2cn.iapi.prodstock.parameter.ReturnStockForOrderBean;
import com.shop2cn.iapi.prodstock.service.ReturnStockForOrderCall;
import com.shop2cn.iapi.agentproducts.parameter.AddAgentProductBean;
import com.shop2cn.iapi.agentproducts.parameter.ProductDetailsDto;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AddAgentProductCallService;
import com.ymttest.business.service.AgentProductCallServiceV2;
import com.ymttest.business.service.GodfreightmgrService;
import com.ymttest.business.service.Shop2cnStockCallService;
import com.ymttest.database.model.GodProductActivityStock;
import com.ymttest.database.sqlwapper.GodProductActivityStockWapper;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.TestDataManager;
/**
*订单取消返还商品库存Test
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_ReturnStockForOrder{
	private static ReturnStockForOrderBean returnstockfororderBean;
	private static ReturnStockForOrderCall returnstockfororderCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("订单取消返还商品库存Test");
		try {
			GodfreightmgrService.deleteBySellerId(rootUserId);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Before
	public void caseUp() {
		returnstockfororderBean=new ReturnStockForOrderBean();
		returnstockfororderCall=new ReturnStockForOrderCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(returnstockfororderCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	// 渠道商
	private static int rootUserId = AgentUsers.rootUserId;
	private static String rootUserName = "NickautoTest";
	// 渠道商->代理商1
	private static int agentUserId1 = AgentUsers.agentUserId1;
	private static String agentUserName1 = "NickautoTest";

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnStockForOrder_001() {
		Logger.start(true,"下单虚拟库存，订单取消，返还库存成功");
		try {
			
			//0真实，30虚拟，下单十件虚拟，之后订单取消，最终0真实，30虚拟
			//新建商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(0);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			//下单虚拟十件
			String catalogId = productDetailsDto.getCatalogList().get(0).getCatalogId();
			long order = Shop2cnStockCallService.InitReduceStockForOrder(rootUserId,catalogId,productId,10);
			productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			
			
			
			
			returnstockfororderBean.setOrderId(order);
			OrderReturnCatalogDto orderReturnCatalogDto = new OrderReturnCatalogDto();
			long RefundBillNo=Long.parseLong(TestDataManager.getRandomNum(10));
			orderReturnCatalogDto.setRefundBillNo(RefundBillNo);
			orderReturnCatalogDto.setOrderId(order);
			orderReturnCatalogDto.setActivityId(0);
			orderReturnCatalogDto.setBuyerId(AgentUsers.csId1);
			orderReturnCatalogDto.setCatalogId(catalogId);
			orderReturnCatalogDto.setCatalogType(0);
			orderReturnCatalogDto.setOrderIsPaid(false);
			orderReturnCatalogDto.setProductId(productId);
			orderReturnCatalogDto.setSellerId(rootUserId);
			orderReturnCatalogDto.setStockNum(10);
			
			
			returnstockfororderBean.setOrderReturnCatalogDto(orderReturnCatalogDto);
			returnstockfororderCall.setData(returnstockfororderBean);
			returnstockfororderCall.callService();
			
			ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			Logger.verifyEquals(30,productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
			Logger.verifyEquals(0,productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
			Logger.verifyEquals(30,productDetailsDto2.getCatalogList().get(0).getVirtualStock(),"验证虚拟库存");
	
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnStockForOrder_002() {
		Logger.start(true,"下单真实库存，订单取消，返还库存成功");
		try {
			
			//30真实，0虚拟，下单十件虚拟，之后订单取消，最终30件真实，0虚拟
			//新建商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(0);
			addAgentProductBean.getCatalogList().get(0).setStock(30);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			//下单真实库存十件
			String catalogId = productDetailsDto.getCatalogList().get(0).getCatalogId();
			long order = Shop2cnStockCallService.InitReduceStockForOrder(rootUserId,catalogId,productId,10);
			productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			
			returnstockfororderBean.setOrderId(order);
			OrderReturnCatalogDto orderReturnCatalogDto = new OrderReturnCatalogDto();
			long RefundBillNo=Long.parseLong(TestDataManager.getRandomNum(10));
			orderReturnCatalogDto.setRefundBillNo(RefundBillNo);
			orderReturnCatalogDto.setOrderId(order);
			orderReturnCatalogDto.setActivityId(0);
			orderReturnCatalogDto.setBuyerId(AgentUsers.csId1);
			orderReturnCatalogDto.setCatalogId(catalogId);
			orderReturnCatalogDto.setCatalogType(0);
			orderReturnCatalogDto.setOrderIsPaid(false);
			orderReturnCatalogDto.setProductId(productId);
			orderReturnCatalogDto.setSellerId(rootUserId);
			orderReturnCatalogDto.setStockNum(10);
			returnstockfororderBean.setOrderReturnCatalogDto(orderReturnCatalogDto);
			returnstockfororderCall.setData(returnstockfororderBean);
			returnstockfororderCall.callService();		

			ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			Logger.verifyEquals(30,productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
			Logger.verifyEquals(30,productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
			Logger.verifyEquals(0,productDetailsDto2.getCatalogList().get(0).getVirtualStock(),"验证虚拟库存");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnStockForOrder_003() {
		Logger.start(true,"验证幂等---下单真实库存，订单取消，返还库存成功");
		try {
			
			//30真实，0虚拟，下单十件虚拟，之后订单取消，最终30件真实，0虚拟
			//新建商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(0);
			addAgentProductBean.getCatalogList().get(0).setStock(30);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			//下单真实库存十件
			String catalogId = productDetailsDto.getCatalogList().get(0).getCatalogId();
			long order = Shop2cnStockCallService.InitReduceStockForOrder(rootUserId,catalogId,productId,10);
			productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			
			returnstockfororderBean.setOrderId(order);
			OrderReturnCatalogDto orderReturnCatalogDto = new OrderReturnCatalogDto();
			long RefundBillNo=Long.parseLong(TestDataManager.getRandomNum(10));
			orderReturnCatalogDto.setRefundBillNo(RefundBillNo);
			orderReturnCatalogDto.setOrderId(order);
			orderReturnCatalogDto.setActivityId(0);
			orderReturnCatalogDto.setBuyerId(AgentUsers.csId1);
			orderReturnCatalogDto.setCatalogId(catalogId);
			orderReturnCatalogDto.setCatalogType(0);
			orderReturnCatalogDto.setOrderIsPaid(false);
			orderReturnCatalogDto.setProductId(productId);
			orderReturnCatalogDto.setSellerId(rootUserId);
			orderReturnCatalogDto.setStockNum(10);
			returnstockfororderBean.setOrderReturnCatalogDto(orderReturnCatalogDto);
			returnstockfororderCall.setData(returnstockfororderBean);
			returnstockfororderCall.callService();		
			returnstockfororderCall.callService();		

			ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			Logger.verifyEquals(30,productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
			Logger.verifyEquals(30,productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
			Logger.verifyEquals(0,productDetailsDto2.getCatalogList().get(0).getVirtualStock(),"验证虚拟库存");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnStockForOrder_004() {
		Logger.start(true,"下单10真实库存，5虚拟库存，订单取消，返还库存成功");
		try {
			
			//10真实，30虚拟，下单十件真实，5虚拟，之后订单取消，最终10真实，30虚拟
			//新建商品
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(10);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			//下单虚拟十件
			String catalogId = productDetailsDto.getCatalogList().get(0).getCatalogId();
			long order = Shop2cnStockCallService.InitReduceStockForOrder(rootUserId,catalogId,productId,10);
			productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			
			returnstockfororderBean.setOrderId(order);
			OrderReturnCatalogDto orderReturnCatalogDto = new OrderReturnCatalogDto();
			long RefundBillNo=Long.parseLong(TestDataManager.getRandomNum(10));
			orderReturnCatalogDto.setRefundBillNo(RefundBillNo);
			orderReturnCatalogDto.setOrderId(order);
			orderReturnCatalogDto.setActivityId(0);
			orderReturnCatalogDto.setBuyerId(AgentUsers.csId1);
			orderReturnCatalogDto.setCatalogId(catalogId);
			orderReturnCatalogDto.setCatalogType(0);
			orderReturnCatalogDto.setOrderIsPaid(false);
			orderReturnCatalogDto.setProductId(productId);
			orderReturnCatalogDto.setSellerId(rootUserId);
			orderReturnCatalogDto.setStockNum(15);
			returnstockfororderBean.setOrderReturnCatalogDto(orderReturnCatalogDto);
			returnstockfororderCall.setData(returnstockfororderBean);
			returnstockfororderCall.callService();		

			ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			Logger.verifyEquals(40,productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
			Logger.verifyEquals(10,productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
			Logger.verifyEquals(30,productDetailsDto2.getCatalogList().get(0).getVirtualStock(),"验证虚拟库存");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ReturnStockForOrder_005() {
		Logger.start(true,"活动商品下单10库存，订单取消，返还库存成功");
		try {
			//活动20件，下单十件，之后订单取消，返还
			AddAgentProductBean addAgentProductBean = AddAgentProductCallService.initAddAgentProduct(rootUserId,
					rootUserName, true);
			addAgentProductBean.getCatalogList().get(0).setVirtualStock(30);
			addAgentProductBean.getCatalogList().get(0).setStock(40);
			String productId = AddAgentProductCallService.AddAgentProduct(addAgentProductBean);
			AddactivityBean initProductaddactivity = AgentProductCallServiceV2.initProductaddactivity(rootUserId, productId,1,20, false);
			Integer activityId = AgentProductCallServiceV2.agentProductaddactivity(initProductaddactivity);
			
			ProductDetailsDto productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			
			//下单虚拟十件
			String catalogId = productDetailsDto.getCatalogList().get(0).getCatalogId();
			long order = Shop2cnStockCallService.InitReduceStockForOrder(rootUserId,activityId,catalogId,productId,10);
			productDetailsDto = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			
			GodProductActivityStock selectByProductId = new GodProductActivityStockWapper().selectByProductIdAndCatalogId(productId, productDetailsDto.getCatalogList().get(0).getCatalogId());
			
			returnstockfororderBean.setOrderId(order);
			OrderReturnCatalogDto orderReturnCatalogDto = new OrderReturnCatalogDto();
			long RefundBillNo=Long.parseLong(TestDataManager.getRandomNum(10));
			orderReturnCatalogDto.setRefundBillNo(RefundBillNo);
			orderReturnCatalogDto.setOrderId(order);
			orderReturnCatalogDto.setActivityId(activityId);
			orderReturnCatalogDto.setBuyerId(AgentUsers.csId1);
			orderReturnCatalogDto.setCatalogId(catalogId);
			orderReturnCatalogDto.setCatalogType(0);
			orderReturnCatalogDto.setOrderIsPaid(false);
			orderReturnCatalogDto.setProductId(productId);
			orderReturnCatalogDto.setSellerId(rootUserId);
			orderReturnCatalogDto.setStockNum(15);
			returnstockfororderBean.setOrderReturnCatalogDto(orderReturnCatalogDto);
			returnstockfororderCall.setData(returnstockfororderBean);
			returnstockfororderCall.callService();		
			
			
			ProductDetailsDto productDetailsDto2 = AddAgentProductCallService.selectProductDetails(rootUserId, productId);
			Logger.verifyEquals(50,productDetailsDto2.getCatalogList().get(0).getStock(),"验证库存");
			Logger.verifyEquals(20,productDetailsDto2.getCatalogList().get(0).getRealStock(),"验证真实库存");
			Logger.verifyEquals(30,productDetailsDto2.getCatalogList().get(0).getVirtualStock(),"验证虚拟库存");
		
			GodProductActivityStock selectByProductId1 = new GodProductActivityStockWapper().selectByProductIdAndCatalogId(productId, productDetailsDto.getCatalogList().get(0).getCatalogId());
			Logger.verifyEquals(selectByProductId.getiStockNum()+10,selectByProductId1.getiStockNum(),"验证虚拟库存");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	
	
}